package com.ruoyi.web.controller.work;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.work.domain.WorkCustomer;
import com.ruoyi.work.service.IWorkCustomerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户资料Controller
 * 
 * @author ruoyi
 * @date 2021-12-21
 */
@Controller
@RequestMapping("/work/customer")
public class WorkCustomerController extends BaseController
{
    private String prefix = "work/customer";

    @Autowired
    private IWorkCustomerService workCustomerService;

    @RequiresPermissions("work:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    /**
     * 查询客户资料列表
     */
    @RequiresPermissions("work:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkCustomer workCustomer)
    {
        startPage();
        List<WorkCustomer> list = workCustomerService.selectWorkCustomerList(workCustomer);
        return getDataTable(list);
    }
    /**
     * 查询客户资料列表
     */
/*    @RequiresPermissions("work:customer:query")*/
    @GetMapping ("/query")
    @ResponseBody
    public AjaxResult query(WorkCustomer workCustomer)
    {
        AjaxResult ajax = new AjaxResult();

        ajax.put("code", 200);

        List<WorkCustomer> list = workCustomerService.selectWorkCustomerList(workCustomer);
        ajax.put("value", list);
        return ajax;
    }
    /**
     * 导出客户资料列表
     */
    @RequiresPermissions("work:customer:export")
    @Log(title = "客户资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkCustomer workCustomer)
    {
        List<WorkCustomer> list = workCustomerService.selectWorkCustomerList(workCustomer);
        ExcelUtil<WorkCustomer> util = new ExcelUtil<WorkCustomer>(WorkCustomer.class);
        return util.exportExcel(list, "客户资料数据");
    }

    /**
     * 新增客户资料
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户资料
     */
    @RequiresPermissions("work:customer:add")
    @Log(title = "客户资料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkCustomer workCustomer)
    {

            WorkCustomer query = new WorkCustomer();
            query.setCustomerName(workCustomer.getCustomerName());
            query.setCreateBy(getLoginName());
            query = workCustomerService.selectWorkCustomerList(query).stream().findFirst().orElse(null);
            if(query!=null){
                return AjaxResult.error("客户已存在，不能重复添加！");
            }
            workCustomer.setCreateBy(getLoginName());

        return toAjax(workCustomerService.insertWorkCustomer(workCustomer));
    }

    /**
     * 修改客户资料
     */
    @RequiresPermissions("work:customer:edit")
    @GetMapping("/edit/{customerId}")
    public String edit(@PathVariable("customerId") Long customerId, ModelMap mmap)
    {
        WorkCustomer workCustomer = workCustomerService.selectWorkCustomerByCustomerId(customerId);
        mmap.put("workCustomer", workCustomer);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户资料
     */
    @RequiresPermissions("work:customer:edit")
    @Log(title = "客户资料", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkCustomer workCustomer)
    {
        return toAjax(workCustomerService.updateWorkCustomer(workCustomer));
    }

    /**
     * 删除客户资料
     */
    @RequiresPermissions("work:customer:remove")
    @Log(title = "客户资料", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workCustomerService.deleteWorkCustomerByCustomerIds(ids));
    }
}
